import { requestcode } from "@/api";
import { requestregister } from "@/api";
import { requestlogin } from "@/api";

import { loginout } from "@/api";
import { token } from "@/api";



const actions = {
    code(context, phone) {
        requestcode(phone).then(function(res) {

            context.commit('CODE', res.data.data)
        })
    },
    loginrequest(context, { phone, password, code }) {
        requestregister(phone, password, code).then(function(res) {
            if (res.data.code == 200) {
                alert('注册成功')
            } else {
                alert('注册失败，请您重试')
            }
        })
    },
    logininfo(context, { phone, password }) {
        requestlogin(phone, password).then(function(res) {
            if (res.data.code == 200) {
                alert('登入成功')


                console.log('用户token：' + res.data.data.token);
                localStorage.setItem('token', res.data.data.token)
            } else {
                alert('登入失败')

            }
        })

    },
    loginout(context, value) {
        loginout().then(function(res) {
            console.log(res);
        })
    },
    tokenrequest(context, value) {
        token().then(function(res) {
            console.log(res.data);
            if (res.data.code == 200) {
                context.commit('TKOEN', res.data.data)
            }

        })
    }



}
const mutations = {
    CODE(state, value) {
        state.codes = JSON.stringify(value)
    },
    TKOEN(state, value) {
        state.getuserinfo = value
        console.log(value);
    }


}
const state = {
    codes: {},
    token: localStorage.getItem('token'),
    getuserinfo: {},


}
const getters = {}

export default {
    actions,
    mutations,
    state,
    getters,

}